<!-- 编辑 基本信息 -->
<template>
	<view class="pb50">
		<uni-card spacing="0" margin="0" :is-shadow="false" :border="false">
			<uni-forms ref="formRef" :model="formData" :rules="formRules" :label-width="150" label-position="top">
				<uni-forms-item label="姓名" name="name" required>
					<view style="line-height: 36px; padding-left: 10px;">{{formData.name}}</view>
				</uni-forms-item>
				<uni-forms-item label="联系电话" name="phone" required>
					<uni-easyinput v-model="formData.phone" placeholder="请输入联系电话" trim />
				</uni-forms-item>
				<uni-forms-item label="联系邮箱" name="email" required>
					<uni-easyinput v-model="formData.email" placeholder="请输入联系邮箱" trim />
				</uni-forms-item>
				<uni-forms-item label="工作单位" name="workUnit" required>
					<uni-easyinput v-model="formData.workUnit" placeholder="请输入工作单位" trim />
				</uni-forms-item>
				<uni-forms-item label="工作岗位" name="workPosition" required>
					<uni-easyinput v-model="formData.workPosition" placeholder="请输入工作岗位" trim />
				</uni-forms-item>
				<uni-forms-item label="技能证书" name="skillsCertificate" required>
					<uni-easyinput v-model="formData.skillsCertificate" placeholder="请输入技能证书" trim />
				</uni-forms-item>
				<uni-forms-item label="擅长领域" name="skilledField" required>
					<uni-easyinput v-model="formData.skilledField" type="textarea" autoHeight placeholder="请输入擅长领域" trim />
				</uni-forms-item>
				<uni-forms-item label="个人简介" name="personalProfile" required>
					<uni-easyinput v-model="formData.personalProfile" type="textarea" autoHeight  placeholder="请输入个人简介" trim />
				</uni-forms-item>
			</uni-forms>
		</uni-card>
	</view>
	<view class="sticky-b0 p10">
		<button @click="submit" type="default" class="basicbutton m10" hover-class="basicbutton-hover">确定修改</button>
	</view>
</template>

<script lang="ts" setup>
	import { Expert } from "@/api/tables/Expert"
	import { getUser, setUser } from "@/utils/storage"
	import { expertDetail, expertEdit } from "@/api/consult"
	import { ref, onBeforeMount } from 'vue'
	let userData = getUser()
	const formData = ref<Expert>({})
	const formRules = ref({
		name: {
			rules: [{
				required: true,
				errorMessage: '请输入姓名'
			}]
		},
		phone: {
			rules: [{
				required: true,
				errorMessage: '请输入联系电话'
			}, {
				pattern: /^[1][3,4,5,7,8][0-9]{9}$/,
				errorMessage: '联系电话格式错误'
			}]
		},
		email: {
			rules: [{
				required: true,
				errorMessage: '请输入联系邮箱'
			}, {
				pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
				errorMessage: '联系邮箱 格式错误',
			}]
		},
		workUnit: {
			rules: [{
				required: true,
				errorMessage: '请输入工作单位'
			}]
		}, 
		workPosition: {
			rules: [{
				required: true,
				errorMessage: '请输入工作岗位'
			}]
		}, 
		skilledField: {
			rules: [{
				required: true,
				errorMessage: '请输入擅长领域'
			}]
		}, 
		skillsCertificate: {
			rules: [{
				required: true,
				errorMessage: '请输入技能证书'
			}]
		}, 
		personalProfile: {
			rules: [{
				required: true,
				errorMessage: '请输入个人简介'
			}]
		}, 
	})
	let formRef = ref(null)
	onBeforeMount(() => {
		getDetail(userData.userId)
	})
	function getDetail(id) {
		expertDetail({ id }).then(res => {
			if (res.code == '200') {
				formData.value = res.data
			}
		})
	}
	


	function submit() {
		formRef.value.validate().then(res => {
			expertEdit({				...userData,
				...formData.value			}).then(res => {
				if (res.code == '200') {
					let user  = { ...userData, ...formData.value}
					setUser(user)
					uni.navigateBack({
						delta: 1
					})
				}
			})
		}).catch(err => {
			uni.showToast({ icon: "none", title: err[0].errorMessage })
		})
	}
</script>

<style lang="scss" scoped>

</style>